home *** CD-ROM | disk | FTP | other *** search
-
- /* ResourcesDemo */
- /* by Ingemar Ragnemalm 1995 */
-
- /* Demonstrates how to use custom resources from your programs. */
-
- /* A demo program that has a resource, stored in itself (should rather be in a */
- /* preference file in a real program) with a single variable, myCount. */
- /* Each time the program is run, the resource is read, myCount is incremented, and */
- /* the program beeps as many times as myCount says. */
-
-
- /* Define a structure and a handle type to it. This is our resource format! */
-
- typedef struct {
- short myCount;
- } MyRecord;
- typedef MyRecord **MyHnd;
-
-
- /* Resource type and number. Use mixed-type names to avoid conflicts */
-
- #define kMyResType 'Demo'
- #define kMyResNum 0
-
-
- MyHnd myResource; /* The handle to our resource */
-
- short i; /* An integer for use as loop variable */
-
-
- static void InitToolbox()
- {
- InitGraf(&qd.thePort);
- InitCursor ();
- MaxApplZone ();
- }; /*InitToolbox*/
-
-
- void main(void) {
-
- InitToolbox();
-
- /**** Get the resource ****/
-
- myResource = (MyHnd)GetResource(kMyResType, kMyResNum);
-
- /**** Create the resource if needed ****/
-
- /* If it didn't load, we assume it doesn't exist. Create it! */
- /* This is done by allocating a handle, initializing the fields and calling AddResource. */
- if ( myResource == 0L )
- {
- myResource = (MyHnd)NewHandle(sizeof(MyRecord)); /* Allocates memory */
- myResource[0]->myCount = 0; /* Init fields */
- AddResource((Handle)myResource, kMyResType, kMyResNum, "\p"); /* Create resource */
- };
-
- /**** Change the resource ****/
-
- (**myResource).myCount++; /* Change the count */
- /* Call ChangedResource to tell MacOS that the resource needs to be written */
- /* back to disk. */
- ChangedResource((Handle)myResource);
-
- /**** Use the resource. Beep as many times as myCount says. ****/
- for ( i = 1 ; i <= (**myResource).myCount ; i++)
- SysBeep(1);
-
- } /* ResourcesDemo */
-